﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using eschoolBO;

namespace ESCHOOL.teachers
{

    public partial class WebForm2 : System.Web.UI.Page
    {
        teacher teach = new teacher(); user_types ut = new user_types();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Panel1.Visible = false;
                for (int i = 1; i < 31; i++)
                {
                    dddate.Items.Add(i.ToString()); ddjdate.Items.Add(i.ToString());
                }
                for (int j = DateTime.Now.Year; j > 1950; j--)
                {
                    ddyear.Items.Add(j.ToString()); ddjyear.Items.Add(j.ToString());
                }
                DataTable dt1=ut.GetRecords(-99,"",false," and ");
                ddusertype.DataSource = dt1;
                ddusertype.DataTextField = "user_type";
                ddusertype.DataValueField= "user_type_id";
                ddusertype.DataBind();


                DataTable dt = teach.GetRecords(Convert.ToInt64(Session["userid"]), "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, "and");
                DetailsView1.DataSource = dt;
                DetailsView1.DataBind();
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            Panel1.Visible = true; Label1.Visible = false; Label2.Visible = false;
            tbusername.Visible = false; tbpassword.Visible = false;
            DetailsView1.Visible = false;
            if (e.CommandName == "Edit")
            {
                DataTable dt = teach.GetRecords(Convert.ToInt64(e.CommandArgument), "", "", "", "", "", "", -99, -99, -99, "", "","","","", false, " and ");
                tbtid.Text = dt.Rows[0]["teacher_id"].ToString();
                tbtname.Text = dt.Rows[0]["teacher_name"].ToString();
                DateTime dat1 = new DateTime();
                dat1 = Convert.ToDateTime(dt.Rows[0]["date_of_birth"].ToString());
                dddate.SelectedValue = dat1.Day.ToString();
                ddmonth.SelectedValue = dat1.Month.ToString();
                ddyear.SelectedValue = dat1.Year.ToString();
                DateTime dat2 = new DateTime();
                dat2 = Convert.ToDateTime(dt.Rows[0]["date_of_join"].ToString());
                ddjdate.SelectedValue = dat2.Day.ToString();
                ddjmonth.SelectedValue = dat2.Month.ToString();
                ddjyear.SelectedValue = dat2.Year.ToString();
                RadioButtonList1.SelectedValue = dt.Rows[0]["gender"].ToString();
                tbphoto.Text = dt.Rows[0]["photo"].ToString();
                Image2.ImageUrl = "~/admin/teacher/" + tbphoto.Text;
                tbaddress.Text = dt.Rows[0]["permanent_address"].ToString();
                tbreno.Text = dt.Rows[0]["residence_pno"].ToString();
                tbmobile.Text = dt.Rows[0]["mobile_pno"].ToString();
                ddusertype.SelectedValue = dt.Rows[0]["user_type_id"].ToString();
                tbmail.Text = dt.Rows[0]["teacher_emailid"].ToString();
                tbusername.Text = dt.Rows[0]["username"].ToString();
                tbpassword.Text = dt.Rows[0]["password"].ToString();
                ddquestion.SelectedValue = dt.Rows[0]["security_question"].ToString();
                tbanswer.Text = dt.Rows[0]["answer"].ToString();
            }
        }


        protected void btnupdate_Click(object sender, EventArgs e)
        {
            teach.teacher_id = Convert.ToInt64(tbtid.Text);
            teach.teacher_name = tbtname.Text;
            DateTime dat1 = new DateTime(Convert.ToInt32(ddyear.SelectedItem.ToString()), Convert.ToInt32(ddmonth.SelectedValue.ToString()), Convert.ToInt32(dddate.SelectedItem.ToString()));
            teach.date_of_birth = dat1.ToString();
            DateTime dat2 = new DateTime(Convert.ToInt32(ddjyear.SelectedItem.ToString()), Convert.ToInt32(ddjmonth.SelectedValue.ToString()), Convert.ToInt32(ddjdate.SelectedItem.ToString()));
            teach.date_of_join = dat2.ToString();
            teach.gender = RadioButtonList1.SelectedValue.ToString();
            if(FileUpload1.HasFile)
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/teacher/"+FileUpload1.FileName));
                teach.photo=FileUpload1.FileName;
            }
            else
                teach.photo=tbphoto.Text;
            teach.permanent_address=tbaddress.Text;
            teach.residence_pno=Convert.ToInt64(tbreno.Text);
            teach.mobile_pno=Convert.ToInt64(tbmobile.Text);
            teach.user_type_id=Convert.ToInt64(ddusertype.SelectedValue.ToString());
            teach.teacher_emailid = tbmail.Text;
            teach.username=tbusername.Text;
            teach.password=tbpassword.Text;
            teach.security_question = ddquestion.SelectedValue.ToString();
            teach.answer = tbanswer.Text;
            teach.isdeleted=false;
            teach.SaveRecords();
            Response.Redirect("~/teachers/TProfile.aspx");
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {

        }

        protected void ddquestion_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
